﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using ChatForum.Business.Entity;
using System.Web.UI.HtmlControls;

namespace ChatForum.Client
{
	public partial class Home : BasePage
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if(User.Identity.IsAuthenticated)
            {
                this.MasterPageFile = "~/MemberLayout.master";
            }
        }
		protected void Page_Load(object sender, EventArgs e)
        {
			categoryPostList.DataSource = ServiceHelper.ServiceHelper.ForumService.GetAllCategory();
			categoryPostList.DataBind();
        }
		protected void OnCategoryItemBound(object sender, ListViewItemEventArgs e)
		{
			if (e.Item.ItemType == ListViewItemType.DataItem)
			{
				Category currentCategory = (Category)e.Item.DataItem;
				ListView postList = (ListView)e.Item.FindControl("categorySpecificPostList");
				var pstListFromDb = ServiceHelper.ServiceHelper.ForumService.GetPostbyCategory(currentCategory.ID, false).OrderByDescending(p => p.CreatedDate).Select(p => new MyPostView(p));
				if (pstListFromDb == null || pstListFromDb.Count() == 0)
					((HtmlImage)e.Item.FindControl("expanderImage")).Style.Add("visibility", "hidden");
				postList.DataSource = pstListFromDb;
				postList.DataBind();
			}
		}
        protected void OnSearchOnlineUser(object sender, EventArgs e)
        {
        }
    }
}